const {
	wxConfigMp,
	wxConfigApp,
	wxConfigH5,
	aliConfigMp,
	aliConfigApp,
	aliConfigH5,
	// aliConfigSandbox
} = require('config')
const uniPay = require('unipay')
const fs = require('fs');
const path = require('path')

module.exports = {
	getPayInstance: (provider, platform) => {
		let uniPayInstance;
		switch (provider + '_' + platform) {
			case 'wxpay_mp-weixin':
				wxConfigMp.pfx = fs.readFileSync(path.join(__dirname, "./apiclient_cert.p12"));
				uniPayInstance = uniPay.initWeixin(wxConfigMp)
				break;
			case 'alipay_mp-alipay':
				uniPayInstance = uniPay.initAlipay(aliConfigMp)
				break;
			case 'wxpay_app-plus':
				uniPayInstance = uniPay.initWeixin(wxConfigApp)
				break;
			case 'alipay_app-plus':
				uniPayInstance = uniPay.initAlipay(aliConfigApp)
				break;
			case 'wxpay_h5':
				uniPayInstance = uniPay.initWeixin(wxConfigH5)
				break;
			case 'alipay_h5':
				uniPayInstance = uniPay.initAlipay(aliConfigH5)
				// uniPayInstance = uniPay.initAlipay(aliConfigSandbox)
				break;
			default:
				break;
		}
		return uniPayInstance;
	},
	getTradeType: (provider, platform) => {
		let tradeType;
		switch (provider + '_' + platform) {
			case 'wxpay_mp-weixin':
				tradeType = 'JSAPI'
				break;
			case 'alipay_mp-alipay':
				break;
			case 'wxpay_app-plus':
				tradeType = 'APP'
				break;
			case 'alipay_app-plus':
				break;
			case 'wxpay_h5':
				tradeType = 'NATIVE'
				break;
			case 'alipay_h5':
				tradeType = 'NATIVE'
				break;
			default:
				return {
					code: -1,
					msg: '参数错误'
				}
		}
		return tradeType;
	}
}
